#include "timeUtils.h"


qint64 TimeUtils::getTimeGen() {
    std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
    auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
    std::time_t timestamp = tmp.count();
    return timestamp;
}


QString TimeUtils::nowDateTimeStr() {
    return QDateTime::currentDateTime().toString(DATETIME_FORMAT);
}


QString TimeUtils::nowDateStr() {
    return QDate::currentDate().toString(DAY_FORMAT);
}


QString TimeUtils::nowTimeStr() {
    return QTime::currentTime().toString(TIME_FORMAT);
}


QString TimeUtils::formatDateTime(QDateTime& dateTime) {
    return dateTime.toString(DATETIME_FORMAT);
}


QString TimeUtils::formatDate(QDate& day) {
    return day.toString(DAY_FORMAT);
}


QString TimeUtils::formatTime(QTime& time) {
    return time.toString(TIME_FORMAT);
}


void TimeUtils::suspendThread(int time) {
    qint64 t1 = getTimeGen();
    while (getTimeGen() - t1 < time) {
        QApplication::processEvents();
    }
}